<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="考试标题" prop="title">
				<el-input v-model="form.title" placeholder="考试标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="主考部门" prop="deptId">
						<select-dept-dialog v-model:deptId="form.deptId" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="选择试卷" prop="questionRecordId">
						<sc-table-select v-model="form.questionRecordId" :apiObj="apiQuestion" :table-width="700" clearable collapse-tags collapse-tags-tooltip :props="props" style="width: 100%">
							<template #header="{form, submit}">
								<el-form :inline="true" :model="form">
									<el-form-item>
										<el-input v-model="form.keyword" placeholder="试卷名称" clearable maxlength="80"></el-input>
									</el-form-item>
									<el-form-item>
										<el-button type="primary" @click="submit">查询</el-button>
									</el-form-item>
								</el-form>
							</template>
							<el-table-column prop="title" label="试卷名称" width="200"></el-table-column>
							<el-table-column prop="remark" label="备注" :show-overflow-tooltip="true"></el-table-column>
						</sc-table-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm" :disabled-date="publishDateAfter" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm" :disabled-date="publishDateAfter" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="考试时长" prop="duration">
						<el-input-number v-model="form.duration" placeholder="考试时长" clearable maxlength="80"></el-input-number>/分钟
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="合格分数" prop="passMark">
						<el-input-number v-model="form.passMark" placeholder="合格分数" clearable maxlength="80"></el-input-number>/分
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="职务权限">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="是否开否" prop="isOpen">
						<el-radio-group v-model="form.isOpen">
							<el-radio-button label="1">开卷</el-radio-button>
							<el-radio-button label="0">闭卷</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="附件权限" prop="attachRole">
						<el-radio-group v-model="form.attachRole">
							<el-radio-button label="1">查看</el-radio-button>
							<el-radio-button label="2">打印</el-radio-button>
							<el-radio-button label="3">下载</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="参考资料" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'exam'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="exam"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from "@/views/module/attachMenu";
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			props: {
				label: 'title',
				value: 'recordId',
				keyword: "keyword"
			},
			mode: "add",
			titleMap: {
				add: '发布测试',
				edit: '编辑测试',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			apiQuestion:this.$API.office.exam.getExamQuestionRecordForSelect,
			templates: [],
			form: {
				recordId: '',
				title: '',
				deptId: '',
				questionRecordId: '',
				beginTime: '',
				endTime: '',
				duration: 0,
				passMark: 0,
				attachRole: '1',
				isOpen: "1",
				userRole: '',
				deptRole: '',
				levelRole: '',
				remark: '',
				attachId: '',
				msgType: '',
			},
			rules: {
				title: [
					{required: true, message: '测试标题不能为空', trigger: 'blur'}
				],
				questionRecordId: [
					{required: true, message: '请选择试卷', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '结束时间不能为空', trigger: 'blur'}
				],
				duration: [
					{required: true, message: '考试时长不能为空', trigger: 'blur'}
				],
				passMark: [
					{required: true, message: '极格分数不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return (time.getTime()+(24*60*60*1000)) < Date.now()
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.exam.insertExamTest.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.exam.updateExamTest.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.deptId = data.deptId
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.duration = data.duration
			this.form.passMark = data.passMark
			this.form.attachRole = data.attachRole
			this.form.isOpen = data.isOpen
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.remark = data.remark
			this.form.attachId = data.attachId
			this.form.msgType = data.msgType
			if(data.questionRecordId!='')
			{
				this.form.questionRecordId = JSON.parse(data.questionRecordId)
			}
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
